﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;
using PagedList;

namespace ShenLanCMS.Web.Controllers
{
    public class LinkController : Controller
    {
        private EntityContext db = new EntityContext();

        int pageSize = 20;
       
        public ActionResult Index()
        {
            return View();
        }

        [IsAdmin]
        public ViewResult Admin(int? page)
        {
            ViewData["actionName"] = "Admin";
            var index = (page ?? 1) - 1;
            var links = db.Links.Where(n => n.Status == 1).OrderBy(n => n.Sort).ThenBy(n => n.AddTime).ToPagedList(index, pageSize);
            return View(links);
        }

        //待审核的友情链接
        [IsAdmin]
        public ViewResult Approve(int? page)
        {
            ViewData["actionName"] = "Approve";
            var index = (page ?? 1) - 1;
            var links = db.Links.Where(n => n.Status == 2).OrderBy(n=>n.Sort).ThenBy(n=>n.AddTime).ToPagedList(index, pageSize);
            return View(links);
        }

        //审核的友情链接
        [HttpPost]
        public ActionResult ApproveLink(Guid id)
        {
            try
            {
                var link = db.Links.Find(id);
                db.Entry(link).State = EntityState.Modified;
                link.Status = 1;
                db.SaveChanges();
                return Json("审核成功");

            }
            catch { return Json("审核失败"); }
        }
 
        //申请友情链接
        public ActionResult Apply()
        {
            return View();
        }

        //申请友情链接
        [HttpPost]
        public ActionResult Apply(Link link)
        {
            if (ModelState.IsValid)
            {
                var links = db.Links.ToList();
                foreach (var item in links)
                {
                    //判断网址是否已经存在
                    if (item.SiteUrl == link.SiteUrl)
                    {
                        if (item.Status == 2)
                            ModelState.AddModelError("SiteUrl", "该网站链接已经提交过申请,请您耐心等待审核");
                        else if (item.Status == 1)
                            ModelState.AddModelError("SiteUrl", "该网站链接已经是本站的友情链接");
                        else
                            ModelState.AddModelError("SiteUrl", "该网站链接已经被拒绝");
                        return View(link);
                    }
                }
                link.LinkID = Guid.NewGuid();
                link.SiteUrl = link.SiteUrl.ToLower();
                if (link.SiteUrl.IndexOf("http://") == -1 && link.SiteUrl.IndexOf("https://") == -1)
                {
                    link.SiteUrl = "http://" + link.SiteUrl;
                }
                link.Status = 2;
                link.AddTime = DateTime.Now;
                link.Sort = 1;
                link.Click = 0;
                db.Links.Add(link);
                db.SaveChanges();
                ModelState.AddModelError("", "提交成功!注:审核成功才被显示.");
                link = new Link();
                return View(link);
            }

            return View(link);
        }

        //添加友情链接
        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        //添加友情链接
        [IsAdmin]
        [HttpPost]
        public ActionResult Create(Link link)
        {
            if (ModelState.IsValid)
            {
                var links = db.Links.ToList();
                foreach (var item in links)
                {
                    //判断网址是否已经存在
                    if (item.SiteUrl == link.SiteUrl)
                    {
                        if (item.Status == 2)
                            ModelState.AddModelError("SiteUrl", "该网站链接已经提交过申请,请您耐心等待审核");
                        else if (item.Status == 1)
                            ModelState.AddModelError("SiteUrl", "该网站链接已经是本站的友情链接");
                        else
                            ModelState.AddModelError("SiteUrl", "该网站链接已经被拒绝");
                        return View(link);
                    }
                }
                link.LinkID = Guid.NewGuid();
                link.SiteUrl = link.SiteUrl.ToLower();
                if (link.SiteUrl.IndexOf("http://") == -1 && link.SiteUrl.IndexOf("https://") == -1)
                {
                    link.SiteUrl = "http://" + link.SiteUrl;
                }
                link.Status = 1;
                link.AddTime = DateTime.Now;
                link.Click = 0;
                db.Links.Add(link);
                db.SaveChanges();
                ModelState.AddModelError("", "添加成功");
                link = new Link();
                return View(link);
            }

            return View(link);
        }

        [IsAdmin]
        public ActionResult Edit(Guid id)
        {
            Link link = db.Links.Find(id);
            return View(link);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(Link link)
        {
            if (ModelState.IsValid)
            {
                db.Entry(link).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Admin");
            }
            return View(link);
        }

        //删除友情链接
        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                Link link = db.Links.Find(id);
                db.Links.Remove(link);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}